<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $user_uid
 * @property string $user_email
 * @property string $user_username
 * @property string $user_truename
 * @property string $user_password
 * @property integer $user_university
 * @property string $user_tags
 * @property string $user_regtime
 * @property string $user_regip
 * @property string $user_lastlogintime
 * @property string $user_lastloginip
 * @property double $user_balance
 * @property integer $user_auth
 * @property integer $user_rating
 * @property integer $user_verified
 * @property string $user_courses
 *
 * The followings are the available model relations:
 * @property Comments[] $comments
 * @property Item[] $items
 * @property Message[] $messages
 * @property Purchase[] $purchases
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_email, user_username, user_truename, user_password, user_courses', 'required'),
			array('user_university, user_auth, user_rating, user_verified', 'numerical', 'integerOnly'=>true),
			array('user_balance', 'numerical'),
			array('user_email, user_username, user_truename', 'length', 'max'=>100),
			array('user_password, user_regip, user_lastloginip', 'length', 'max'=>45),
			array('user_tags', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_uid, user_email, user_username, user_truename, user_password, user_university, user_tags, user_regtime, user_regip, user_lastlogintime, user_lastloginip, user_balance, user_auth, user_rating, user_verified, user_courses', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comments', 'comm_user'),
			'items' => array(self::HAS_MANY, 'Item', 'item_seller'),
			'messages' => array(self::HAS_MANY, 'Message', 'msg_reciever'),
			'purchases' => array(self::HAS_MANY, 'Purchase', 'purchase_buyer'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_uid' => 'User Uid',
			'user_email' => 'Email',
			'user_username' => 'Username',
			'user_truename' => 'Truename',
			'user_password' => 'Password',
			'user_university' => 'University',
			'user_tags' => 'User Tags',
			'user_regtime' => 'User Regtime',
			'user_regip' => 'User Regip',
			'user_lastlogintime' => 'User Lastlogintime',
			'user_lastloginip' => 'User Lastloginip',
			'user_balance' => 'User Balance',
			'user_auth' => 'User Auth',
			'user_rating' => 'User Rating',
			'user_verified' => 'User Verified',
			'user_courses' => 'Courses',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_uid',$this->user_uid);
		$criteria->compare('user_email',$this->user_email,true);
		$criteria->compare('user_username',$this->user_username,true);
		$criteria->compare('user_truename',$this->user_truename,true);
		$criteria->compare('user_password',$this->user_password,true);
		$criteria->compare('user_university',$this->user_university);
		$criteria->compare('user_tags',$this->user_tags,true);
		$criteria->compare('user_regtime',$this->user_regtime,true);
		$criteria->compare('user_regip',$this->user_regip,true);
		$criteria->compare('user_lastlogintime',$this->user_lastlogintime,true);
		$criteria->compare('user_lastloginip',$this->user_lastloginip,true);
		$criteria->compare('user_balance',$this->user_balance);
		$criteria->compare('user_auth',$this->user_auth);
		$criteria->compare('user_rating',$this->user_rating);
		$criteria->compare('user_verified',$this->user_verified);
		$criteria->compare('user_courses',$this->user_courses,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
  public function validatePassword($password)
  {
		return $this->hashPassword($password)===$this->user_password;
  }

  public function hashPassword($password)
  {
		return $password;
  }
}